import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isString from "lodash/isString";
import _isObject from "lodash/isObject";

var _dec, _class;

/**
 *通用导入模块
 * @since 2018-9-12
 * @version 0.0.1

 */
import React from 'react';
import { Button, Upload } from 'antd';
import { Bind } from 'lodash-decorators';
import request from 'utils/request';
import { API_HOST } from 'utils/config';
import notification from 'utils/notification';
import intl from 'utils/intl';

function isJSON(str) {
  var result;

  try {
    result = JSON.parse(str);
  } catch (e) {
    return false;
  }

  return _isObject(result) && !_isString(result);
}

var UploadExcel = (_dec = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(UploadExcel, _React$Component);

  var _super = _createSuper(UploadExcel);

  function UploadExcel() {
    var _this;

    _classCallCheck(this, UploadExcel);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      uploadLoading: false
    };
    return _this;
  }

  _createClass(UploadExcel, [{
    key: "beforeUpload",
    value: function beforeUpload(file) {
      var _this2 = this;

      var _this$props = this.props,
          args = _this$props.args,
          sync = _this$props.sync,
          auto = _this$props.auto,
          prefixPatch = _this$props.prefixPatch,
          templateCode = _this$props.templateCode,
          _this$props$param = _this$props.param,
          param = _this$props$param === void 0 ? {} : _this$props$param,
          _this$props$success = _this$props.success,
          success = _this$props$success === void 0 ? function (e) {
        return e;
      } : _this$props$success,
          tenantId = _this$props.tenantId;
      var formData = new FormData();
      formData.append('excel', file, file.name);

      if (args) {
        formData.append('param', JSON.stringify(args));
      }

      if (prefixPatch && templateCode) {
        // eslint-disable-next-line no-nested-ternary
        var url = auto ? "".concat(API_HOST).concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/sync/auto-import?templateCode=").concat(templateCode) : sync ? "".concat(API_HOST).concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/sync/data-upload?templateCode=").concat(templateCode) : "".concat(API_HOST).concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/data-upload?templateCode=").concat(templateCode);
        this.setState({
          uploadLoading: true
        });
        request(url, {
          method: 'POST',
          query: param,
          body: formData,
          responseType: 'text'
        }).then(function (res) {
          if (isJSON(res) && JSON.parse(res).failed) {
            notification.error({
              description: JSON.parse(res).message
            });
          } else if (res) {
            success(res);
          }
        }).catch(function (e) {
          console.log(e);
        }).finally(function () {
          _this2.setState({
            uploadLoading: false
          });
        });
      }

      return false;
    }
  }, {
    key: "render",
    value: function render() {
      var uploadLoading = this.state.uploadLoading;
      var _this$props$disabled = this.props.disabled,
          disabled = _this$props$disabled === void 0 ? false : _this$props$disabled;
      var uploadProps = {
        accept: '.xls,.xlsx,.csv',
        beforeUpload: this.beforeUpload,
        showUploadList: false,
        style: {
          margin: '0 12px'
        }
      };
      return /*#__PURE__*/React.createElement(Upload, uploadProps, /*#__PURE__*/React.createElement(Button, {
        className: "label-btn",
        icon: "upload",
        loading: uploadLoading,
        disabled: disabled
      }, intl.get("cimp.comment.view.button.dataUpload").d('数据上传')));
    }
  }]);

  return UploadExcel;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "beforeUpload", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "beforeUpload"), _class.prototype)), _class));
export { UploadExcel as default };